package com.example.shellshopping.login;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shellshopping.R;
import com.example.shellshopping.utils.BaseFragment;
import com.example.shellshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册界面
 */
public class RegisterFragment extends BaseFragment {
    EditText telET,nicknameET,pwdET,repwdET,questionET,anwserET;
    Button ensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        telET = view.findViewById(R.id.frag_reg_et_tel);
        nicknameET = view.findViewById(R.id.frag_reg_et_name);
        pwdET = view.findViewById(R.id.frag_reg_et_pwd);
        repwdET = view.findViewById(R.id.frag_reg_et_rePwd);
        questionET = view.findViewById(R.id.frag_reg_et_question);
        anwserET = view.findViewById(R.id.frag_reg_et_anwser);
        ensureBtn = view.findViewById(R.id.frag_reg_btn);
        ensureBtn.setOnClickListener(listener);
    }
    View.OnClickListener listener = new View.OnClickListener(){
        @Override
        public void onClick(View v){
            String tel = telET.getText().toString();
            String nickname = nicknameET.getText().toString();
            String pwd = pwdET.getText().toString();
            String repwd = repwdET.getText().toString();
            String question = questionET.getText().toString();
            String answer = anwserET.getText().toString();
            boolean flag = judgeInput(tel,nickname,pwd,repwd,question,answer);
            if (flag) {  //可以执行网络请求
                Map<String,String>map = new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                map.put("name","");
                map.put("address","");
                map.put("nickname",nickname);
                map.put("question",question);
                map.put("answer",answer);
                postNetwork(URLUtils.register_url,map);

            }
        }
    };
    @Override
    public void onSuccess(String result){
        super.onSuccess(result);
        //解析数据
        UserBean bean = new Gson().fromJson(result,UserBean.class);
        //
        int code = bean.getCode();
        switch(code){
            case 200:  //注册成功
                Toast.makeText(getContext(),"注册成功！",Toast.LENGTH_SHORT).show();
                break;
            case 201:  //手机号已注册
                Toast.makeText(getContext(),"手机号码已注册请，重新登录！",Toast.LENGTH_SHORT).show();
                break;
            default:   //其他，注册失败
                Toast.makeText(getContext(),"注册失败！",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /* 判断输入的内容是否符合内容 */
    private boolean judgeInput(String tel,String nickname,String pwd,String repwd,String quention,String answer) {
        //判断输入内容是否为空
        if (TextUtils.isEmpty(tel)||TextUtils.isEmpty(nickname)||
                TextUtils.isEmpty(pwd)||TextUtils.isEmpty(repwd)||
                TextUtils.isEmpty(quention)||TextUtils.isEmpty(answer)) {
            Toast.makeText(getContext(),"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        //判断两次输入密码是否相同
        if (!pwd.equals(repwd)) {
            Toast.makeText(getContext(),"两次输入密码不相同",Toast.LENGTH_SHORT).show();
            return false;
        }
        //判断输入的电话号码是否符合要求
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        boolean matches = tel.matches(regex);
        if(!matches){
            Toast.makeText(getContext(),"输入手机号不合法",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

}